<?php


namespace app\api\controller\xilujob;


use app\common\controller\Api;
use app\common\model\xilujob\Third;
use app\common\model\xilujob\PackageOrder;
use PHPQRCode\QRcode;
use think\Exception;
use function fast\array_except;
use function fast\array_get;

class Pay extends Api
{
    protected $noNeedRight = ['*'];

    /**
     * 支付
     */
    public function pay(){
        $params = $this->request->param('');
        $platform = array_get($params,'platform','wxmini');
        $order_id = array_get($params,'order_id');
        $pay_type = array_get($params,'pay_type');
        if(!$order_id || !$pay_type){
            $this->error('参数错误');
        }
        $config = get_addon_config('xilujob');
        if(!$config || !$config['mch_id'] || !$config['mch_key']){
            $this->error("请正确配置微信商户信息");
        }
        $orderinfo = PackageOrder::get($order_id);
        if(!$orderinfo || $orderinfo->pay_status == 2){
            $this->error("订单不存在或已支付完毕");
        }
        $orderinfo->pay_type = $pay_type;
        if($pay_type == 3){
            $params = array_except($params, ['corporate_company','open_bank','corporate_account']);
        }else if($params['pay_type'] == 1 && $platform == 'wxmini'){
            $third = Third::where('id',$params['third_id'])->find();
            if(!$third){
                $this->error('未找到登录信息');
            }
            $params['openid'] = $third->openid;
            unset($params['third_id']);
        }
        $params['order_trade_no'] = 'PG'.date('YmdHis').mt_rand(10,9999);
        $orderinfo->save(array_except($params, ['order_id']));
        $order_no = $orderinfo['order_trade_no'];
        $pay_price = $orderinfo['pay_price'];
        if($pay_type == 1){
            #微信支付
            $order = [
                'body'      =>  '购买招聘套餐',
                'order_no'  =>  $order_no,
                'pay_price' =>  $pay_price,
                //notify_url' =>  $notify_url,
            ];
            try {
                if($platform == 'pc'){
                    $wx = new \addons\xilujob\controller\WeixinPublic();
                    $order['trade_type'] = 'NATIVE';
                    $order['notify_url'] = request()->domain().'/addons/xilujob/weixin_public/notify';
                    $result = $wx->union_order($order);
                    if(!$result){
                        $this->error('创建微信订单失败');
                    }
                    #生成支付二维码
                    $root = '.';
                    $filename = md5(mt_rand()).'.png';
                    $qrcodePath = '/uploads/qrcode';
                    if (!file_exists($root . $qrcodePath)) {
                        @mkdir($root . $qrcodePath, 0777, true);
                    }
                    $qrcode = $qrcodePath.$filename;
                    QRcode::png($result['code_url'], $root.$qrcode, 'L', 6, 2);
                    $orderinfo->code_url = request()->domain().$qrcode;
                    $orderinfo->code_url_expire_in = time()+3600;
                    $orderinfo->save();
                    $result = ['platform'=>'pc','code_url'=>$orderinfo->code_url];
                }else{
                    $wx = new \addons\xilujob\controller\WeixinMini();
                    $order['openid'] = $orderinfo['openid'];
                    $order['trade_type'] = 'JSAPI';
                    $order['notify_url'] = request()->domain().'/addons/xilujob/weixin_mini/notify';
                    $result = $wx->union_order($order);
                }
            }catch (Exception $e){
                $this->error($e->getMessage());
            }
            $this->success('创建微信订单成功',$result);

        }else if($pay_type == 3){
            $this->success("支付类型已修改");
        }else{
            $this->success("暂不支持当前支付类型");
        }


    }

}